<?php

namespace App\Http\Controllers\Admin\Member\Purchaser;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Area;
use App\B2BProject\AttachmentPrivateFiles;
use App\B2BProject\Custom\Business\Store;
use App\B2BProject\Custom\Member;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PurchaserController extends Controller
{
    use AdminControllerBuildTrait;

    protected $moduleName = '';

    /**
     * 采购商列表
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

            $query = Member::where('class', 2)->orderBy('created_at', 'desc');  // 2、采购商   1、供应商

            if ($user = $request->query('username')) {
                if ($request->query('select_val') == 1) { //用户名
                    $query = $query->where('user', 'like', "%$user%");
                } else { //公司名
                    $query->whereHas('information', function ($query) use ($user) {
                        $query->where('company', 'like', "$user");
                    });
                }
            }
            if ($county_id = $request->query('county')) {
                $query->whereHas('information', function ($query) use ($county_id) {
                    $query->where('county_id', $county_id);
                });
            }
            if ($city_id = $request->query('city')) {

                $query->whereHas('information', function ($query) use ($city_id) {
                    $query->where('city_id', $city_id);
                });
            }
            if ($province_id = $request->query('province')) {
                $query->whereHas('information', function ($query) use ($province_id) {
                    $query->where('province_id', $province_id);
                });
            }

            if ($start = $request->query('start_time')) {  //下单时间
                $query = $query->where('created_at', '>=', date('Y-m-d', strtotime($start)));
            }
            if ($end = $request->query('end_time')) {
                $query = $query->where('created_at', '<=', date('Y-m-d', strtotime($end)));
            }

            $members = $query->paginate($request->query('pageSize', 7));

            $data = [];
            foreach ($members as $member) {
                $item   = [
                    'id'          => $member->id,
                    'username'    => $member->user,
                    'companyname' => $member->information->company,
                    'district'    => Area::getName($member->information->province_id) . ' ' . Area::getName($member->information->city_id),
                    /*'application_date' => date('Y-m-d', strtotime($member->created_at)), //店铺的申请时间*/
                    'storestate'  =>date('Y-m-d', strtotime($member->created_at)), // 1 开启  0 拒绝  假数据
                    'editorurl'   => route('admin.purchaser.edit', [$member->id]),
                    'deleurl'     => route('admin.purchaser.destroy', [$member->id]),
                ];
                $data[] = $item;
            }
            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $members->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $members->total(),
                ],
            ]);
        }
        return view('admin/member/purchaser/index');
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $member = Member::where('id', $id)->first();

        return view('admin/member/purchaser/edit')->with('member', $member);  //此处用来页面渲染数据
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if ($password = $request->input('password')) {
            $member           = Member::findOrNew($id);
            $member->password = \Hash::make($password);
            $member->save();
        }
        return $this->index($request);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $store = Member::findOrFail($id);
        $store->delete();

        return $this->index($request);
    }

    /**
     * 批量删除  循环遍历id进行删除
     * @param Request $request
     * @return mixed
     * @throws ModelNotFoundException
     * @throws \Exception
     */
    public function batchRemove(Request $request)
    {
        try {
            return \DB::transaction(function() use ($request) {
                $arr = $request->input('ids');
                foreach ($arr as $member_id) {
                    $member = Member::findOrFail($member_id);
                    $member->delete();
                }
                return $this->index($request);
            });
        } catch (ModelNotFoundException $e) {
            throw $e;
        }
    }

    /**
     * 证件资料的信息
     * @param $id
     * @return string
     */
    static public function fileInfo($id)
    {
        $member  = Member::where('id', $id)->first();
        $legal_b = [

            'img'   => AttachmentPrivateFiles::where('id',
                $member->information->legal_identity_b)->first()->adminRoute(),
            'img_s' => AttachmentPrivateFiles::where('id',
                $member->information->legal_identity_b)->first()->adminRoute(),
            'text'  => '身份证(正面)',
        ];

        $legal_a  = [

            'img'   => AttachmentPrivateFiles::where('id',
                $member->information->legal_identity_a)->first()->adminRoute(),
            'img_s' => AttachmentPrivateFiles::where('id',
                $member->information->legal_identity_a)->first()->adminRoute(),
            'text'  => '身份证(背面)',
        ];
        $business = [

            'img'   => AttachmentPrivateFiles::where('id',
                $member->information->business_license)->first()->adminRoute(),
            'img_s' => AttachmentPrivateFiles::where('id',
                $member->information->business_license)->first()->adminRoute(),
            'text'  => '营业执照副本',
        ];

        $data = [$legal_a, $legal_b, $business];

        return json_encode($data);
    }

    /**
     * 假数据 TODO 后期需要修改
     * @return string
     */
    static public function condition()
    {
        $data = [
            [
                'code' => 1,
                'text' => '用户名'
            ],
            [
                'code' => 2,
                'text' => '公司名称'
            ]
        ];

        return json_encode($data);
    }


}
